<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>apache的rewrite规则 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>apache的rewrite规则</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;rewrite的规则：   </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">      RewriteEngine on</div><div class="line">      RewriteCond %&#123;HTTP_USER_AGENT&#125;  ^.*Firefox/4.0* [NC,OR]</div><div class="line">      RewriteCond %&#123;HTTP_USER_AGENT&#125;  ^.*Tomato Bot/1.0* [NC]</div><div class="line">      RewriteCond   %&#123;REQUEST_URI&#125; !^/404*</div><div class="line">      RewriteRule  .*  /404.html </div><div class="line">    &lt;/IfModule&gt;</div><div class="line">   &lt;IfModule mod_rewrite.c&gt;</div><div class="line">        RewriteEngine on</div><div class="line">        RewriteCond %&#123;HTTP_USER_AGENT&#125;  ^*Firefox/4.0* [NC,OR]</div><div class="line">        RewriteCond %&#123;HTTP_USER_AGENT&#125;  ^*Tomato Bot/1.0* [NC]</div><div class="line">        RewriteRule  .*  -  [F]</div><div class="line">    &lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<ul>
<li>RewriteEngine on                     #打开rewirte功能</li>
<li>RewriteCond %{变量} 正则 [标志]</li>
<li>RewriteRule 正则1  正则2</li>
<li>正则1一般是从www.<em>*</em>.com/这个以后开始的</li>
<li><code>- [F]</code>:   这里的-表示替换</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;标志：</p>
<ol>
<li><a href="force redirect">R=301</a> 强制外部重定向<br>强制在替代字符串加上<a href="http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定，将用缺省的302" target="_blank" rel="external">http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定，将用缺省的302</a> HTTP状态码。</li>
<li>[F]禁用URL,返回403HTTP状态码。<br>30 <a href="force URL to be gone">G</a> 强制URL为GONE，返回410HTTP状态码。</li>
<li><a href="force proxy">P</a> 强制使用代理转发。</li>
<li><a href="last rule">L</a> 表明当前规则是最后一条规则，停止分析以后规则的重写。</li>
<li><a href="next round">N</a> 重新从第一条规则开始运行重写过程。</li>
<li><a href="chained with next rule">C</a> 与下一条规则关联</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果规则匹配则正常处理，该标志无效，如果不匹配，那么下面所有关联的规则都跳过。</p>
<ol>
<li>[T]=MIME-type(force MIME type) </li>
<li>[NS] (used only if no internal sub-request) 只用于不是内部子请求</li>
<li><a href="no case">NC</a> 不区分大小写</li>
<li><a href="query string append">QSA</a> 追加请求字符串</li>
<li><a href="no URI escaping of output">NE</a> 不在输出转义特殊字符</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed</p>
<ol>
<li><a href="pass through to next handler">PT</a> 传递给下一个处理</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">RewriteRule ^/abc(.*) /def<span class="variable">$1</span> [PT] <span class="comment"># 将会交给/def规则处理</span></div><div class="line">   Alias /def /ghi</div></pre></td></tr></table></figure>
<ol>
<li><a href="skip next rule(s">S=num</a>) 跳过num条规则</li>
<li><a href="set environment variable">E=VAR:VAL</a> 设置环境变量</li>
<li>[OR]     或者           两个RewriteCond 之间什么都不写就是     且</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;变量：</p>
<ul>
<li>%{HTTP_USER_AGENT}      表示：访问的user_agent</li>
<li>%{HTTP_HOST}    表示：当前访问的网址，只是指前缀部分，格式是www.xxx.com不包括“<a href="http://”和“/”" target="_blank" rel="external">http://”和“/”</a></li>
<li>%{REQUEST_URI}     表示：表示访问的相对地址，就是相对根目录的地址，就是域名/后面的成分，格式上包括最前面的“/”，</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;www.123.com/xiang/1.html</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;RewriteRule:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">RewriteRule ^/(.*)$ http://www.123.com/<span class="variable">$1</span> [R=301,L]</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/58. apache的rewrite规则/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
